package com.englishtest.base;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.ServletRequestUtils;


/**
 * Created by IntelliJ IDEA.
 * User: xiaomin
 * Date: 2006-8-8
 * Time: 11:48:36
 * To change this template use File | Settings | File Templates.
 */
public class PageHelper {
    private static final int defaultMaxLinkedPages = 10;
    private static final int defaultPageSize = 20;

    public static Paginater getDefaultPaginater(HttpServletRequest request) {
        return getPaginater(request, true, defaultPageSize);
    }

    public static Paginater getPaginater(HttpServletRequest request, boolean needTotalCount, int pageSize) {
        int currPage = getCurrentPage(request);
        return new Paginater(needTotalCount, pageSize, currPage);
    }
    
    /**
     * Get the current page from request parameter. First get page from 
     * "page" parameter, if not valid, get page from hand write page 
     * parameter, if also not valid, return 0
     * @param request
     * @return
     */
    public static int getCurrentPage(HttpServletRequest request) {
    	int page = -1;
    	String strPage = request.getParameter("page");
    	if(strPage != null) {
			try {
				page = Integer.parseInt(strPage);
			} catch (Exception e) {
			}    		
    	}
    	
    	if(page != -1)  return page;
    	
        page = ServletRequestUtils.getIntParameter(request, "w_page", -1);
        if(page < 0)  return 0;
        return page - 1;
    }
    
    public static void adjustPageFromCount(Paginater paginater) {
    	int page = paginater.getTotalCount() / paginater.getPageSize() - 1;
    	if(page < 0) page = 0;
    	paginater.setPage(page);
    }

    /**
     * Return the first page to which create a link around the current page.
     */
    public static int getFirstLinkedPage(int currentPage, int pageCount, int maxLinkedPages) {
        return Math.max(0, Math.min(pageCount - maxLinkedPages, currentPage - maxLinkedPages / 2));
    }

    public static int getFirstLinkedPage(int currentPage, int pageCount) {
        return Math.max(0, Math.min(pageCount - defaultMaxLinkedPages, currentPage - defaultMaxLinkedPages / 2));
    }

    /**
     * Return the last page to which create a link around the current page.
     */
    public static int getLastLinkedPage(int currentPage, int pageCount, int maxLinkedPages) {
        return Math.min(getFirstLinkedPage(currentPage, pageCount, maxLinkedPages) + maxLinkedPages - 1, pageCount - 1);
    }

    public static int getLastLinkedPage(int currentPage, int pageCount) {
        return Math.min(getFirstLinkedPage(currentPage, pageCount, defaultMaxLinkedPages) + defaultMaxLinkedPages - 1, pageCount - 1);
    }

    public static int getMaxPage(int totalCount, int pageSize) {
        return (totalCount + pageSize -1) / pageSize - 1;
    }

    public static Map getPageProperty(Paginater paginater) {
        Map pageProperty = new HashMap();
        pageProperty.put("paginater", paginater);
        pageProperty.put("firstLinkedPage", new Integer(PageHelper.getFirstLinkedPage(paginater.getPage(), paginater.getPageCount(), defaultMaxLinkedPages)));
        pageProperty.put("lastLinkedPage", new Integer(PageHelper.getLastLinkedPage(paginater.getPage(), paginater.getPageCount(), defaultMaxLinkedPages)));

        return pageProperty;
    }
}
